package com.gec.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gec.domain.User;
import com.gec.service.UserService;
import com.gec.service.impl.UserServiceImpl;

@WebServlet("/update.action")
public class UpdateUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		/*
		 * 1 超链接加上 update?id=1 2 写一个updateServlet 1)接收id的值
		 * 2)创建UserService对象，调用getUserById(Integer id) 3)如果用户不为空,把用户信息存储到request对象中
		 * 3)跳转到showUpdate
		 */
		String sid = request.getParameter("id");
		if (!"".equals(sid) && sid != null) {
			Integer id = Integer.parseInt(sid);
			UserService service = new UserServiceImpl();
			User user = service.getUserById(id);
			// 如果不为空
			if (user != null) {
				out.println("<html>");
				out.println("<head>");
				out.println("<link rel='stylesheet' href='css/bootstrap.css'>");
				out.println("</head>");
				out.println("<body>");
				out.println("<form action='showUpdate.action' method='post'  class='form-horizontal'>");
				
				// 隐藏域
				out.println("<input type='hidden' name='id' value='" + user.getId() + "'><br/>");
				

				out.println("<div class='form-group'>");
				out.println("<label class='control-label col-md-3'>用户名</label>");
				out.println("<div class='col-md-3'>");
				out.println("<input type='text' name='name' value='" + user.getName() + "' class='form-control' />");
				out.println("</div>");
				out.println("</div>");

				out.println("<div class='form-group'>");
				out.println("<label class='control-label col-md-3'>密码</label>");
				out.println("<div class='col-md-3'>");
				out.println("<input type='text' name='password' value='" + user.getPassword() + "' class='form-control' />");
				out.println("</div>");
				out.println("</div>");
				
				String sex = user.getSex();

				String check = "";
				String check2 = "";
				if (sex.equals("男")) {
					check = "checked";
				} else {
					check2 = "checked";
				}

				out.println("<div class='form-group'>");
				out.println("<label class='control-label col-md-3'>性别</label>");
				out.println("<div class='col-md-3'>");
				out.println("<div class='radio'>");
				out.println("<label>");
				out.println("<input type='radio' name='sex' value='男' " + check + ">男");
				out.println("</label>");
				out.println("<label>");
				out.println("<input type='radio'  name='sex' value='女' " + check2 + ">女");
				out.println("</label>");
				out.println("</div>");
				out.println("</div>");
				out.println("</div>");


				
				out.println("<div class='form-group'>");
				out.println("<div class='col-md-3'></div>");
				out.println("<div class='col-md-3'><button class='btn btn-success'>修改</button></div>");
				out.println("</div>");
				out.println("</form>");
				out.println("</body>");
				out.println("</html>");

			}
		}
	}

}
